package mess;

public class LeeCode_38 {
    public static void main(String[] args) {
        System.out.println(countAndSay(12));
    }

    private static String countAndSay(int n) {
        if (n == 1)
            return "1";
        StringBuilder sb = new StringBuilder();
        String s = countAndSay(n - 1);
        int len = s.length();
        char c1 = s.charAt(0);
        int count = 1;
        if (len == 1){
            sb.append((char) (count + 48));
            sb.append(c1);
        }
        for (int i = 1; i < len; i++) {
            char c2 = s.charAt(i);
            if (i == len - 1){
                if (c1 == c2){
                    count++;
                    sb.append((char) (count + 48));
                    sb.append(c1);
                    continue;
                }else {
                    sb.append((char) (count + 48));
                    sb.append(c1);

                    sb.append("1");
                    sb.append(c2);
                    continue;
                }
            }
            if (c1 != c2){
                sb.append((char) (count + 48));
                sb.append(c1);
                c1 = c2;
                count = 1;
            }else
                count++;
        }
        return sb.toString();
    }
}
